<!--
 * @Description: ------------ fileDescription -----------
 * @Author: snows_l snows_l@163.com
 * @Date: 2024-08-14 10:00:17
 * @LastEditors: snows_l snows_l@163.com
 * @LastEditTime: 2025-01-17 19:58:49
 * @FilePath: /BLOG/src/views/play/video/index.vue
-->
<template>
  <div class="video-out-warp">
    <PageTopCover
      :moduleTitle="'影视'"
      :icon="'icon-shipin'"
      :coverImg="getOnlineImgUrl('/imgs/bg/121.png')"
      :isMobile="isMobi"
      :mudulDesc="'本站所有影视均来源于互联网，仅供学习交流使用，请勿用于任何商业用途！'"
      :isArticle="false"></PageTopCover>
    <div class="video-container" :class="{ 'm-video-container': isMobi }">
      <div class="center-max-width-warp">
        <div class="video-content-warp" :style="{ margin: state.page.total <= state.page.size ? '20px 0' : '20px 0 0 0' }">
          <div class="video-warp-list" v-if="state.list.length > 0">
            <div
              class="video-item pointer intersection-observer kbn-custom hover-shadow"
              :data-tip="'点击播放 ' + item.title"
              @click="handlevideo(item)"
              v-for="item in state.list">
              <div class="video-ifarme-warp">
                <LImg class="video-img" :src="item.cover.includes('http') ? item.cover : getCoverImg(item.cover)" :isUnPreview="true" />
                <i class="iconfont icon-bofang"></i>
              </div>
              <div class="video-item-title">{{ item.title }}</div>
            </div>
          </div>
          <div class="no-article" v-else>
            <Empty :text="'暂无影视资源，期待您的分享~'" :loadingText="'视频正在拼命加载中...'" :loading="state.loading" />
          </div>

          <LoadingMore
            v-if="store.projectConfig.pageType == 'more'"
            :loading="state.loading"
            :hasMore="state.isMore"
            :length="state.list.length"
            @loadMore="handleLoadMore"></LoadingMore>
          <CustomPage v-else :page="state.page" @change="handlePageChange" :loading="state.loading" scorllTop="video-container"></CustomPage>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { getZoneList } from '@/api/zone';
import useIntersectionObserver from '@/hooks/useIntersectionObserver';
import useResize from '@/hooks/useResize';
import { useAppStore } from '@/store/app';
import { getOnlineImgUrl } from '@/utils/common';
import { reactive } from 'vue';

const store = useAppStore();
const { isMobi } = useResize();
useIntersectionObserver();

/**
 *
 * @description: 影视列表
 * @param { Object }  Item { url: String, title: String, cover: String }
 * @param { String }  bvid 视频的bvid 必填
 * @param { String }  cid 视频的cid bvid有了可以不填
 * @param { Number }  as_wide 是否宽屏 1: 宽屏, 0: 小屏
 * @param { Number }  high_quality 是否高清 1: 高清, 0: 最低视频质量(默认) 如视频有 360p 720p 1080p 三种, 默认或者 high_quality=0 是最低 360p high_quality=1 是最高1080p
 * @param { Number }  danmaku 是否开启弹幕 1: 开启(默认), 0: 关闭
 * @param { Number }  t 视频的开始时间
 * @param { Number }  p ｜page 视频的播放页码
 * @param { Boolean }  autoplay 视频的自动播放
 */
const state = reactive({
  loading: false,
  isMore: true,
  list: [],
  page: {
    page: 1,
    size: 10,
    total: 0
  }
});

const getCoverImg = (name: String) => {
  return new URL(`../../../assets/images/bg/${name}`, import.meta.url).href;
};

const getZoneListFn = scorllTopCallback => {
  state.loading = true;
  getZoneList({ ...state.page, type: 1 })
    .then(res => {
      if (res.code == 200) {
        res.data.forEach(item => {
          let imgs = (item.imgs && item.imgs.split(',')) || [];
          item.url = `https://player.bilibili.com/player.html?bvid=${item.remark}&p=1&as_wide=1&high_quality=1&danmaku=0&t=0&autoplay=1`;
          item.cover = import.meta.env.VITE_CURRENT_ENV == 'dev' ? import.meta.env.VITE_DEV_BASE_SERVER + imgs[0] : store.projectConfig.bsServerUrl + imgs[0];
          item.title = item.text;
        });

        if (store.projectConfig.pageType == 'more') {
          state.list = [...state.list, ...res.data];
        } else {
          state.list = res.data;
        }
        state.page.total = res.total;
        if (state.list.length >= res.total) {
          state.isMore = false; // 已经没有更多数据了
        }
        scorllTopCallback && scorllTopCallback();
        state.loading = false;
      }
    })
    .finally(() => {
      state.loading = false;
    });
};
getZoneListFn(false);

// 加在更多
const handleLoadMore = () => {
  state.loading = true;
  state.page.page++;
  getZoneListFn(false);
};

// 分页改变
const handlePageChange = (page: number, size: number, scorllTopCallback: Function) => {
  state.page.page = page;
  state.page.size = size;
  getZoneListFn(scorllTopCallback);
};

const handlevideo = (row: any) => {
  let urlPrefix = store.projectConfig.blogUrl;
  window.open(urlPrefix + '/play/mp4/playing?url=' + row.url, '_blank');
};
</script>

<style lang="scss" scoped>
.video-out-warp {
  width: 100%;
  height: 100%;
  width: 100%;
  height: 100%;
  min-height: 100vh;
  .video-container {
    width: 100%;
    height: 100%;
    background-color: var(--bg-image-warp-color);
    .center-max-width-warp {
      height: 100%;
      max-width: var(--content-max-width);
      margin: 0 auto;
      .shengming-warp {
        font-size: 16px;
        margin: 10px 0;
        .text {
          color: var(--text-color);
          line-height: 40px;
          text-align: center;
        }
      }
      .video-content-warp {
        // background-color: var(--bg-content-color);
        border-radius: var(--border-radius-2);
        .video-warp-list {
          display: flex;
          flex-wrap: wrap;
          justify-content: space-between;
          .video-item {
            border-radius: var(--border-radius-2);
            width: 435px;
            height: 330px;
            background-color: #fff;
            box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
            margin: 10px;
            display: flex;
            flex-direction: column;
            align-items: center;
            position: relative;
            overflow: hidden;
            &:hover {
              .video-img {
                transform: scale(1.1);
              }
            }
            .video-img {
              transition: all 0.6s ease-in-out;
            }
            .video-ifarme-warp {
              width: 100%;
              height: 300px;
              overflow: hidden;
              transition: all 1.2s ease-in-out;
              position: relative;
              .iconfont {
                position: absolute;
                left: 50%;
                top: 50%;
                transform: translate(-50%, -50%);
                color: #fff;
                font-size: 50px;
                opacity: 0;
                transition: all 0.3s ease-in-out;
              }
              &:hover {
                .iconfont {
                  opacity: 1;
                }
              }
            }
            .video-item-title {
              width: 100%;
              font-size: 16px;
              font-weight: bold;
              text-align: center;
              color: var(--text-color);
              display: -webkit-box;
              -webkit-box-orient: vertical;
              -webkit-line-clamp: 1; /* 定义文本的行数 */
              overflow: hidden;
              text-overflow: ellipsis;
              height: 30px;
              line-height: 30px;
              background-color: var(--bg-content-color);
              transition: bottom 0.3s ease-in-out;
            }
          }
        }
      }
    }
  }
  .m-video-container {
    .center-max-width-warp {
      width: var(--content-max-width-m-more) !important;
      .video-item {
        height: 230px !important;
        width: 100% !important;
        margin: 10px 0 !important;
        .video-ifarme-warp {
          height: 200px !important;
        }
        .video-item-title {
        }
      }
    }
  }
}
</style>
